package com.example.healthmanagement.service;

import com.example.healthmanagement.dao.FoodRemRepository;
import com.example.healthmanagement.dao.HealthAnaRepository;
import com.example.healthmanagement.entity.CookBook;
import com.example.healthmanagement.entity.HealthAnalysize;
import com.example.healthmanagement.entity.vo.HealthAnaVo;
import com.example.healthmanagement.pojo.AnalyzePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HealthAnaService {

    private HealthAnaRepository healthAnaRepository;

    @Autowired
    public HealthAnaService(HealthAnaRepository healthAnaRepository) {
        this.healthAnaRepository = healthAnaRepository;
    }

    public boolean add(HealthAnalysize healthAnalysize) {
        if (healthAnaRepository.save(healthAnalysize) != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean update(HealthAnalysize healthAnalysize) {
        healthAnaRepository.save(healthAnalysize);
        return true;
    }

    public List<HealthAnaVo> findAll(String name) {
        List<HealthAnalysize> healthAnalysizes = new ArrayList<>();
        if (name != null) {
            healthAnalysizes = healthAnaRepository.findHealthAnalysizesByDeletedAndAnalysizeNameContaining(false, name);
        } else {
            healthAnalysizes = healthAnaRepository.getHealthAnalysizesByDeleted(false);
        }
        List<HealthAnaVo> healthVos = new ArrayList<>();
        for (HealthAnalysize healthAnalysize : healthAnalysizes) {
            HealthAnaVo healthAnaVo = getHealthAnaVo(healthAnalysize);
            healthVos.add(healthAnaVo);
        }
        return healthVos;
    }

    private static HealthAnaVo getHealthAnaVo(HealthAnalysize healthAnalysize) {
        HealthAnaVo healthAnaVo = new HealthAnaVo();
        healthAnaVo.setId(healthAnalysize.getId());
        healthAnaVo.setAnalysizeName(healthAnalysize.getAnalysizeName());
        healthAnaVo.setAnalysizeContent(healthAnalysize.getAnalysizeContent());
        healthAnaVo.setCreatedAt(healthAnalysize.getCreatedAt());
        healthAnaVo.setLastBmi(healthAnalysize.getLastBmiLeft() + " ~ " + healthAnalysize.getLastBmiRight());
        healthAnaVo.setLastBmiLeft(healthAnalysize.getLastBmiLeft());
        healthAnaVo.setLastBmiRight(healthAnalysize.getLastBmiRight());
        healthAnaVo.setNowBmiLeft(healthAnalysize.getNowBmiLeft());
        healthAnaVo.setNowBmiRight(healthAnalysize.getNowBmiRight());
        healthAnaVo.setNowBmi(healthAnalysize.getNowBmiLeft() + " ~ " + healthAnalysize.getNowBmiRight());
        healthAnaVo.setDeleted(healthAnalysize.getDeleted());
        return healthAnaVo;
    }

    public HealthAnalysize findById(int id) {
        return healthAnaRepository.findHealthAnalysizeByIdAndDeleted(id, false);
    }

    public HealthAnalysize analyze(AnalyzePojo data) {
        return healthAnaRepository.findByBmi(data.getLastBmi(), data.getNowBmi());
    }
}
